package com.twu.biblioteca.item;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Enrico on 10/09/2014.
 */
public class Items<E extends Item> {

    private List<E> items;

    public Items(){
        items = new ArrayList<E>();
    }

    public Items(List<E> items) {
        this.items = items;
    }

    public boolean isEmpty() {
        return items.isEmpty();
    }

    public void add(E item) {
        if (item == null) throw new NullPointerException("Item cannot be null");
        items.add(item);
    }

    public void remove(E item){
        if (item == null) throw new NullPointerException("Item cannot be null");
        items.remove(item);
    }

    public E lookup(String chosenTitle){
        for(E item : items){
            if(item.matches(chosenTitle)){
                return item;
            }
        }
        return null;
    }

    public void list(){
        boolean showHeader = true;
        for(E item : items){
            if(showHeader){
                item.showHeader();
                showHeader = false;
            }
            item.show();
        }
        System.out.println();
    }
}
